/*     */ package com.yc.dao;
/*     */ 
/*     */ import btsm.bean.dbConnector;
/*     */ import com.yc.dao.model.FileBO;
/*     */ import com.yc.dao.model.FolderBO;
/*     */ import com.yc.util.ExtTreeNode;
/*     */ import com.yc.util.UtilBtsm;
/*     */ import java.io.File;
/*     */ import java.sql.ResultSet;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.StringTokenizer;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ 
/*     */ public class FolderBODAO
/*     */ {
/*  34 */   private static final Log log = LogFactory.getLog(FolderBODAO.class);
/*     */   public static final String FOLDER_NAME = "folderName";
/*     */   public static final String PARENT_ID = "parentId";
/*     */   public static final String USER_ID = "userId";
/*     */   public static final String DEPTH = "depth";
/*     */   public static final String OTHER1 = "other1";
/*     */   public static final String OTHER2 = "other2";
/*  43 */   private dbConnector dbcon = null;
/*  44 */   private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/*     */ 
/*     */   private void dbConInit() {
/*  47 */     if (this.dbcon == null)
/*  48 */       this.dbcon = new dbConnector();
/*     */   }
/*     */ 
/*     */   public void dbConClose() {
/*  52 */     if (this.dbcon != null)
/*  53 */       this.dbcon.closeAll();
/*  54 */     this.dbcon = null;
/*     */   }
/*     */ 
/*     */   protected void initDao()
/*     */   {
/*     */   }
/*     */ 
/*     */   public String getFolderPath(dbConnector dbdon, String strParentFolderID)
/*     */   {
/*  69 */     log.debug("查询父文件夹路径");
/*  70 */     String strSQL = "select other1 from folder where folderID = '" + strParentFolderID + "'";
/*  71 */     ResultSet rs = null;
/*  72 */     String strParentFolderPath = null;
/*     */     try
/*     */     {
/*  75 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/*  76 */       rs = dbdon.execQuery(strSQL);
/*  77 */       if (rs.next())
/*  78 */         strParentFolderPath = rs.getString("other1");
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*  82 */       e.printStackTrace();
/*     */     }
/*  84 */     return strParentFolderPath;
/*     */   }
/*     */ 
/*     */   public String getFolderPath(String strFolderID)
/*     */   {
/*  89 */     log.debug("查询父文件夹路径");
/*  90 */     dbConInit();
/*  91 */     String strSQL = "select other1 from folder where folderID = '" + strFolderID + "'";
/*  92 */     ResultSet rs = null;
/*  93 */     String strParentFolderPath = null;
/*     */     try
/*     */     {
/*  96 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/*  97 */       rs = this.dbcon.execQuery(strSQL);
/*  98 */       if (rs.next())
/*  99 */         strParentFolderPath = rs.getString("other1");
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 103 */       e.printStackTrace();
/*     */     }
/* 105 */     dbConClose();
/* 106 */     return strParentFolderPath;
/*     */   }
/*     */ 
/*     */   public void save(FolderBO bo)
/*     */   {
/* 111 */     dbConInit();
/* 112 */     log.debug("saving FolderBO instance");
/*     */     try
/*     */     {
/* 115 */       StringBuffer sqlBuffer = new StringBuffer();
/* 116 */       sqlBuffer.append("insert into folder(folderID,folderName,parentID,userID,createTime,depth,other1) ");
/* 117 */       sqlBuffer.append(" values ( ");
/* 118 */       sqlBuffer.append("'" + bo.getFolderId() + "',");
/* 119 */       sqlBuffer.append("'" + bo.getFolderName() + "',");
/* 120 */       sqlBuffer.append("'" + bo.getParentId() + "',");
/* 121 */       sqlBuffer.append("'" + bo.getUserId() + "',");
/* 122 */       sqlBuffer.append("'" + this.sdf.format(bo.getCreateTime()) + "',");
/* 123 */       sqlBuffer.append("'" + bo.getDepth() + "',");
/* 124 */       sqlBuffer.append("'" + bo.getOther1() + "')");
/* 125 */       String addsql = sqlBuffer.toString();
/* 126 */       log.info(addsql);
/*     */ 
/* 129 */       this.dbcon.execUpdate(addsql);
/* 130 */       log.debug("save successful");
/*     */     } catch (Exception re) {
/* 132 */       log.error("save failed", re);
/* 133 */       re.printStackTrace();
/*     */     }
/* 135 */     dbConClose();
/*     */   }
/*     */ 
/*     */   public List findByUserId(String userId, String strParentFolderID, String strDepth)
/*     */   {
/* 140 */     log.info("根据用户名查询文件夹");
/* 141 */     dbConInit();
/* 142 */     String strSQL = "select folderID,folderName from folder where userID = '" + userId + "' and depth = '" + strDepth + "' and parentID = '" + strParentFolderID + "'";
/* 143 */     List list = new ArrayList();
/* 144 */     ResultSet rs = null;
/*     */     try
/*     */     {
/* 147 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/* 148 */       rs = this.dbcon.execQuery(strSQL);
/* 149 */       while (rs.next())
/*     */       {
/* 151 */         ExtTreeNode extTreeNode = new ExtTreeNode();
/* 152 */         extTreeNode.setId(rs.getString("folderID"));
/* 153 */         extTreeNode.setText(rs.getString("folderName"));
/* 154 */         extTreeNode.setDisabled(false);
/* 155 */         extTreeNode.setAllowDrag(false);
/* 156 */         extTreeNode.setLeaf(false);
/* 157 */         extTreeNode.setChecked(Boolean.valueOf(false));
/* 158 */         list.add(extTreeNode);
/*     */       }
/* 160 */       rs.close();
/*     */     }
/*     */     catch (Exception e) {
/* 163 */       e.printStackTrace();
/*     */     }
/* 165 */     dbConClose();
/* 166 */     return list;
/*     */   }
/*     */ 
/*     */   public List findComFolder(String strParentFolderID)
/*     */   {
/* 178 */     log.info("根据用户名查询文件夹");
/* 179 */     dbConInit();
/* 180 */     String strSQL = "select folderID,folderName from folder where  parentID = '" + strParentFolderID + "'";
/* 181 */     List list = new ArrayList();
/* 182 */     ResultSet rs = null;
/*     */     try
/*     */     {
/* 185 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/* 186 */       rs = this.dbcon.execQuery(strSQL);
/* 187 */       while (rs.next())
/*     */       {
/* 189 */         ExtTreeNode extTreeNode = new ExtTreeNode();
/* 190 */         extTreeNode.setId(rs.getString("folderID"));
/* 191 */         extTreeNode.setText(rs.getString("folderName"));
/* 192 */         extTreeNode.setDisabled(false);
/* 193 */         extTreeNode.setAllowDrag(false);
/* 194 */         extTreeNode.setLeaf(false);
/* 195 */         extTreeNode.setChecked(Boolean.valueOf(false));
/* 196 */         list.add(extTreeNode);
/*     */       }
/* 198 */       rs.close();
/*     */     }
/*     */     catch (Exception e) {
/* 201 */       e.printStackTrace();
/*     */     }
/* 203 */     dbConClose();
/* 204 */     return list;
/*     */   }
/*     */ 
/*     */   public boolean deleteFolder(String strFolderID)
/*     */   {
/* 215 */     log.info("删除【" + strFolderID + "】文件夹 ");
/* 216 */     dbConInit();
/* 217 */     String strSQL = "delete from folder where folderID = '" + strFolderID + "'";
/* 218 */     boolean returnFlag = true;
/*     */     try
/*     */     {
/* 221 */       String strFolderPath = getFolderPath(this.dbcon, strFolderID);
/* 222 */       UtilBtsm.deleteFolder(strFolderPath);
/* 223 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/* 224 */       this.dbcon.execUpdate(strSQL);
/*     */     }
/*     */     catch (Exception e) {
/* 227 */       returnFlag = false;
/*     */     }
/* 229 */     dbConClose();
/* 230 */     log.info("删除结束 ");
/* 231 */     return returnFlag;
/*     */   }
/*     */ 
/*     */   private String getNewFolderPath(String strOlderFolderPath, String strFolderName)
/*     */   {
/* 237 */     log.info("strOlderFolderPath = [" + strOlderFolderPath + "]");
/* 238 */     StringTokenizer token = new StringTokenizer(strOlderFolderPath, "\\");
/*     */ 
/* 241 */     String[] ary = new String[token.countTokens()];
/* 242 */     int count = 0;
/* 243 */     while (token.hasMoreTokens())
/*     */     {
/* 245 */       ary[(count++)] = token.nextToken();
/*     */     }
/*     */ 
/* 248 */     ary[(ary.length - 1)] = strFolderName;
/* 249 */     StringBuffer buf = new StringBuffer();
/* 250 */     for (int i = 0; i < ary.length; i++)
/*     */     {
/* 252 */       buf.append(ary[i]);
/* 253 */       if (i < ary.length - 1)
/* 254 */         buf.append("\\\\");
/*     */     }
/* 256 */     return buf.toString();
/*     */   }
/*     */ 
/*     */   private List getFileIDAndPath(dbConnector dbdon, String strFolderID)
/*     */   {
/* 261 */     log.debug("查询文件夹下所有文件");
/* 262 */     String strSQL = "select fileId,filePath from filedetail where folderId = '" + strFolderID + "'";
/* 263 */     ResultSet rs = null;
/* 264 */     List list = new ArrayList();
/*     */     try
/*     */     {
/* 267 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/* 268 */       log.info(strSQL);
/* 269 */       rs = dbdon.execQuery(strSQL);
/* 270 */       while (rs.next())
/*     */       {
/* 272 */         FileBO bo = new FileBO();
/* 273 */         bo.setFileId(rs.getString("fileId"));
/* 274 */         bo.setFilePath(rs.getString("filePath"));
/* 275 */         list.add(bo);
/*     */       }
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 280 */       e.printStackTrace();
/*     */     }
/* 282 */     return list;
/*     */   }
/*     */ 
/*     */   private String newFilePath(String strOldFilePath, String strFolderPath)
/*     */   {
/* 287 */     String shortName = strOldFilePath.substring(strOldFilePath.lastIndexOf("\\") + 1);
/* 288 */     String strReturn = strFolderPath + "\\\\" + shortName;
/* 289 */     log.info("strReturn = [" + strReturn + "]");
/* 290 */     return strReturn;
/*     */   }
/*     */ 
/*     */   public boolean updateFolderName(FolderBO bo)
/*     */   {
/* 298 */     log.info("修改文件夹【" + bo.getFolderId() + "】名称");
/* 299 */     dbConInit();
/*     */ 
/* 302 */     boolean returnFlag = true;
/*     */     try
/*     */     {
/* 305 */       String strOlderFolderPath = getFolderPath(this.dbcon, bo.getFolderId());
/* 306 */       File file = new File(strOlderFolderPath);
/* 307 */       String strNewFolderPath = getNewFolderPath(strOlderFolderPath, bo.getFolderName());
/* 308 */       file.renameTo(new File(strNewFolderPath));
/* 309 */       String strSQL = "update folder set folderName= '" + bo.getFolderName() + "', other1 = '" + strNewFolderPath + "' " + 
/* 310 */         "where folderID = '" + bo.getFolderId() + "'";
/* 311 */       strSQL = new String(strSQL.getBytes("ISO8859-1"), "UTF-8");
/* 312 */       log.info(strSQL);
/* 313 */       this.dbcon.execUpdate(strSQL);
/* 314 */       List list = getFileIDAndPath(this.dbcon, bo.getFolderId());
/* 315 */       if (list.size() > 0)
/*     */       {
/* 317 */         for (int i = 0; i < list.size(); i++)
/*     */         {
/* 319 */           FileBO fileBO = (FileBO)list.get(i);
/* 320 */           strSQL = "update filedetail set filePath= '" + newFilePath(fileBO.getPath(), strNewFolderPath) + "'" + 
/* 321 */             " where fileId = '" + fileBO.getFileId() + "'";
/*     */ 
/* 323 */           log.info(strSQL);
/* 324 */           this.dbcon.execUpdate(strSQL);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 333 */       e.printStackTrace();
/* 334 */       returnFlag = false;
/*     */     }
/* 336 */     dbConClose();
/* 337 */     log.info("修改结束 ");
/* 338 */     return returnFlag;
/*     */   }
/*     */ 
/*     */   public String queryMaxFolderID()
/*     */   {
/* 355 */     dbConInit();
/* 356 */     ResultSet rs = null;
/* 357 */     String strQueryMaxSQLWhere = "";
/* 358 */     String strQueryMaxSQL = "";
/* 359 */     String strMaxNo = "";
/* 360 */     String strMatch = "";
/* 361 */     String strDjJc = "Folder";
/* 362 */     long lLshCd = 4L;
/*     */     try
/*     */     {
/* 366 */       for (int i = 0; i < lLshCd; i++)
/*     */       {
/* 368 */         strMatch = strMatch + "_";
/*     */       }
/*     */ 
/* 371 */       Date nowTime = new Date();
/* 372 */       SimpleDateFormat fmtDate = null;
/* 373 */       fmtDate = new SimpleDateFormat("yyyyMMdd");
/* 374 */       String strDate = fmtDate.format(nowTime);
/* 375 */       strQueryMaxSQLWhere = strDjJc + strDate;
/* 376 */       strQueryMaxSQL = "select max(folderId) as strMaxNo from folder where folderId like '" + strQueryMaxSQLWhere + 
/* 377 */         strMatch + "' ";
/*     */ 
/* 379 */       strQueryMaxSQL = new String(strQueryMaxSQL.getBytes("ISO8859-1"), "UTF-8");
/* 380 */       log.debug("strQueryMaxSQL = [" + strQueryMaxSQL + "]");
/* 381 */       rs = this.dbcon.execQuery(strQueryMaxSQL);
/* 382 */       if (rs.next())
/*     */       {
/* 384 */         strMaxNo = rs.getString("strMaxNo");
/*     */       }
/* 386 */       if (rs != null)
/*     */       {
/* 388 */         rs.close();
/* 389 */         rs = null;
/*     */       }
/*     */ 
/* 393 */       if ((strMaxNo == null) || (strMaxNo.equals("")) || 
/* 394 */         (strMaxNo.length() != strQueryMaxSQLWhere.length() + lLshCd))
/*     */       {
/* 396 */         for (int i = 1; i < lLshCd; i++)
/*     */         {
/* 398 */           strQueryMaxSQLWhere = strQueryMaxSQLWhere + "0";
/*     */         }
/* 400 */         strMaxNo = strQueryMaxSQLWhere + "1";
/* 401 */         log.debug("是第一个编号=" + strMaxNo);
/*     */       }
/*     */       else
/*     */       {
/* 407 */         long lLshValue = Long.valueOf(
/* 408 */           strMaxNo.substring((int)(strMaxNo.length() - lLshCd), strMaxNo.length())).longValue() + 1L;
/*     */ 
/* 410 */         String strLshBh = String.valueOf(lLshValue);
/*     */ 
/* 412 */         for (int i = strLshBh.length(); i < lLshCd; i++)
/*     */         {
/* 414 */           strLshBh = "0" + strLshBh;
/*     */         }
/* 416 */         strMaxNo = strQueryMaxSQLWhere + strLshBh;
/* 417 */         log.debug("如果不是第一个编号=" + strMaxNo);
/*     */       }
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 422 */       e.printStackTrace();
/*     */       try
/*     */       {
/* 428 */         if (rs != null)
/*     */         {
/* 430 */           rs.close();
/* 431 */           rs = null;
/*     */         }
/*     */       }
/*     */       catch (Exception ex)
/*     */       {
/* 436 */         ex.printStackTrace();
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 428 */         if (rs != null)
/*     */         {
/* 430 */           rs.close();
/* 431 */           rs = null;
/*     */         }
/*     */       }
/*     */       catch (Exception ex)
/*     */       {
/* 436 */         ex.printStackTrace();
/*     */       }
/*     */     }
/* 439 */     dbConClose();
/* 440 */     return strMaxNo;
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.dao.FolderBODAO
 * JD-Core Version:    0.6.2
 */